namespace ScrumSprintMonitor.UI.Wpf.Views.MonitorWindow
{
	#region #using Directives

	using System;
	using System.ComponentModel;
	using System.Windows;
	using System.Windows.Controls;
	using System.Windows.Media;
	using System.Windows.Media.Animation;
	using System.Windows.Threading;

	using ViewModels;

	#endregion

	public class TeamMemberBadgeBase : UserControl
	{
		private Border glassPanel;
		private ITeamMemberViewModel teamMemberViewModel;

		private void OnDataContextChanged()
		{
			if (this.teamMemberViewModel != DataContext)
			{
				if (this.teamMemberViewModel != null)
				{
					this.teamMemberViewModel.PropertyChanged -= OnTeamMemberViewModelPropertyChanged;
					this.teamMemberViewModel = null;
				}

				this.teamMemberViewModel = DataContext as ITeamMemberViewModel;
				if (this.teamMemberViewModel != null)
				{
					this.teamMemberViewModel.PropertyChanged += OnTeamMemberViewModelPropertyChanged;
				}
			}
		}

		protected void OnGlassPanelLoaded(object sender, RoutedEventArgs e)
		{
			this.glassPanel = (Border) sender;
			this.glassPanel.DataContextChanged += OnGlassPanelDataContextChanged;

			OnDataContextChanged();

			// Remove any databinding
			if (Background == null)
			{
				this.glassPanel.Background = (this.teamMemberViewModel != null
				                              	? new SolidColorBrush(this.teamMemberViewModel.BackgroundColor)
				                              	: Brushes.Transparent);
			}
			else
			{
				this.glassPanel.Background = this.glassPanel.Background.CloneCurrentValue();
			}
		}

		private void OnGlassPanelDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
		{
			OnDataContextChanged();
		}

		private void OnTeamMemberViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			switch (e.PropertyName)
			{
				case "BackgroundColor":
					Dispatcher.BeginInvoke(new Action<Border, Color>(BeginAnimation), DispatcherPriority.Background, this.glassPanel,
					                       this.teamMemberViewModel.BackgroundColor);
					break;
			}
		}

		private static void BeginAnimation(Border control, Color targetColor)
		{
			// Prevent problems with a frozen value
			control.Background = control.Background.CloneCurrentValue();

			ColorKeyFrame keyFrame = new EasingColorKeyFrame(targetColor, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(5.0)), new QuarticEase
			                                                                                                               	{
			                                                                                                               		EasingMode =
			                                                                                                               			EasingMode.
			                                                                                                               			EaseInOut
			                                                                                                               	});

			control.Background.BeginAnimation(SolidColorBrush.ColorProperty, new ColorAnimationUsingKeyFrames
			                                                                 	{
			                                                                 		KeyFrames =
			                                                                 			{
			                                                                 				keyFrame
			                                                                 			}
			                                                                 	});
		}
	}
}